// pages/index/generate_order/generate_order.js
const utilFile = require("../../../utils/util.js")
const formatTime = utilFile.formatTime;
const toastFile = require("../../../utils/notice.js");
const toast = toastFile.Toast;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyItems: "",
    pickerStartDate: "",
    pickerEndDate: "",
    delayedDays: 30,
    pickerStartTime: "08:00",
    pickerEndTime: "23:00",
    useDate: "",
    useTime: "08:00",
    returnDate: "",
    returnTime: "08:00",
  },

  onLoad: function (options) {
    const items = options.items;

    // 页面加载时获取今天的日期，为picker起始日期，往后30天为结束日期
    const startDate = new Date();
    const startDateStr = this.formatDateForPicker(startDate);
    var delayedDays = this.data.delayedDays;
    const endDate = this.getDelayedDate(startDate, delayedDays);
    const endDateStr = this.formatDateForPicker(endDate);

    this.setData({
      applyItems: items,
      pickerStartDate: startDateStr,
      pickerEndDate: endDateStr,
      useDate: startDateStr,
      returnDate: startDateStr,
    })


  },

  formatDateForPicker: function(date) {
    var pickerStartYear = date.getFullYear();
    var pickerStartMonth = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth();
    var pickerStartDay = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return pickerStartYear + "-" + pickerStartMonth + "-" + pickerStartDay;
  },

  getDelayedDate: function(date, days) {
    return new Date(date.getTime() + days * 24 * 3600 * 1000);
  },

  // 使用日期变化
  useDateChanged: function(e) {
    const useDate = e.detail.value;
    this.setData({
      useDate: useDate,
    })
  },
  
  useTimeChanged: function(e) {
    const useTime = e.detail.value;
    this.setData({
      useTime: useTime,
    })
  },

  returnDateChanged: function(e) {
    const returnDate = e.detail.value;
    this.setData({
      returnDate: returnDate,
    })
  },

  returnTimeChanged: function(e) {
    const returnTime = e.detail.value;
    this.setData({
      returnTime: returnTime,
    })
  },

  handleTimeSelected: function(e) {
    const useDate = this.data.useDate;
    const useTime = this.data.useTime;
    const returnDate = this.data.returnDate;
    const returnTime = this.data.returnTime;
    var use = this.formatStrToDate(useDate, useTime);
    var comeback = this.formatStrToDate(returnDate, returnTime);
    if (use.getTime() >= comeback.getTime()) {
      toast.notify("归还时间不能小于使用时间！");
      return;
    } else {
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.emit("onTimeSelected", {
        useDate: useDate + " " + useTime,
        returnDate: returnDate + " " + returnTime,
      });
      wx.navigateBack({
        delta: 1,
      })
    }


  },

  formatStrToDate: function(date, time) {
    var dateStrs = date.split("-");
    var year = parseInt(dateStrs[0], 10);
    var month = parseInt(dateStrs[1], 10) - 1;
    var date = parseInt(dateStrs[2], 10);
    var timeStrs = time.split(":");
    var hour = parseInt(timeStrs[0], 10);
    var minite = parseInt(timeStrs[0], 10);
    var returnDate = new Date(year, month, date, hour, minite);
    return returnDate;
  }
})